<!DOCTYPE html>
<html lang="zh-CN">
<head>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta charset="UTF-8">
	<meta name="robots" content="noarchive">
	<meta name="robots" content="none">
	<title>接力任务</title>
	<meta content="width=620,user-scalable=no" name="viewport">
	<link rel="stylesheet/less" href="/admin/css/forms.less">
<script src="/public/plugins/layer/layer.js"></script>
</head>

<body>

<script src="/js/ajax141212.js"></script>
<script src="/js/RelayTask.js?1084605351"></script>

    <div class="box public-padding-top-bottom" id="infopanel" style="margin:0px auto;width:100%;">
        <div>任务总数：13</div>
        <div>任务进度：0</div>
    </div>

    <br>
    <div class="box public-padding-top-bottom" style="margin:0px auto; width:100%;">
        <div id="showinfo" style="width:100%; height:200px; border:1px solid #CCC; overflow-y:auto"></div>
        <div style="height:30px; line-height:30px;text-align:center">
            <button id="button_start" type="button" class="btn btn-primary" name="submit-button">开始</button>
            <button id="button_stop" type="button" class="btn btn-primary" name="submit-button">停止</button>
        </div>
    </div>

<script language="javascript">

    var total = 13;

    var mt = new RelayTask(0);

    mt.onTask = function(id, contain){
        console.log("onTask:"+id+" "+contain);

        new Ajax().get({
            url : "/v1/search/topic/async-collect"+"?id="+id+"&contain="+contain,
            timeout : 20000,
            resultType : "json",
            onLoaded : function(result){
                console.log(result);

                if(result.code == 1){
                    mt.number++;
                    var msg = "任务："+result.data.id+" "+result.data.title+" 完成！";

                    addinfo(msg, '#009900');
                    $($('#infopanel').children('div').get(1)).html("任务进度："+mt.number+"/"+total);
                    mt.next(result.execute_id);
                }else if(result.code == 2){
                    addinfo("任务全部完成！", '#009900');
                }else{
                    addinfo(result.info+"失败");
                    addinfo("任务 "+key+" 超时，5秒后自动重试");
                    mt.retry();
                }
            },
            onTimeout : function(){
                addinfo("任务["+(key+1)+"] 《"+"》 超时，5秒后自动重试", '#FF0000');
                setTimeout(function(){
                    mt.retry();
                }, 5000);
            }
        });


    }

    function addinfo(info, color){
        if(!arguments[1])color = '#000000';

        console.log(info);

        var text = $('#showinfo').html();
        info = '<div style="color:'+color+'">'+info+'</div>';
        text = text + info;

        $('#showinfo').html(text);
        $('#showinfo')[0].scrollTop = $('#showinfo')[0].scrollHeight;//滚动条在最下方
    }

    document.getElementById("button_start").onclick = function(){
        addinfo("任务开始：", '#009900');
        mt.start();
    }

    document.getElementById("button_stop").onclick = function(){
        mt.stop();
    }

    mt.onEnd = function(){
        addinfo("任务完成！", '#009900');
    };

    (function(){
        $($('#infopanel').children('div').get(0)).html("任务总数："+total);

    })();

</script>

</body>
</html>